package com.ruoyi.web.controller.workSafetyStandardization.inspection;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workSafetyStandardization.Vo.inspection.InspectionAddVo;
import com.ruoyi.workSafetyStandardization.domain.InspectionResult;
import com.ruoyi.workSafetyStandardization.service.IInspectionResultService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 标准化考核汇总Controller
 *
 * @author pengjie
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/secure/inspectionResult")
public class InspectionResultController extends BaseController
{
    @Autowired
    private IInspectionResultService inspectionResultService;

    /**
     * 查询标准化考核汇总列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionResult inspectionResult, @Param("pageSize") String pageSize, @Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<InspectionResult> list = inspectionResultService.selectInspectionResultList(inspectionResult);
        return getDataTable(list);
    }

    /**
     * 通过时间,查询标准化考核汇列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:query')")
    @GetMapping("/query")
//    public AjaxResult query(@Param("year") String year,@Param("inspectionState") Long integer)
    public TableDataInfo query(@Param("time") String time,@Param("inspectionState") String state,@Param("pageSize") String pageSize, @Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        System.out.println(time.getClass() +" "+ time);
        InspectionResult inspectionResult =new InspectionResult();
        if ("".equals(state)|| state==null){
            state= "1";
        }
        inspectionResult.setInspectionState(Long.valueOf(state));
        String date_string = time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        try {
            Date date = sdf.parse(date_string);
            System.out.println(date);
            inspectionResult.setCreateAt(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  getDataTable(inspectionResultService.queryInspectionResultListByDate(inspectionResult));
    }

    /**
     * 通过id预览信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:query')")
    @GetMapping("/getInspectionTaskInfo")
    public AjaxResult preview(@Param("InspectionId") String InspectionId )
    {
        return  success(inspectionResultService.queryInspectionResultListByInspectionId(InspectionId));
    }

    /**
     * 导出标准化考核汇总列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:export')")
    @Log(title = "标准化考核汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionResult inspectionResult)
    {
        List<InspectionResult> list = inspectionResultService.selectInspectionResultList(inspectionResult);
        ExcelUtil<InspectionResult> util = new ExcelUtil<InspectionResult>(InspectionResult.class);
        util.exportExcel(response, list, "标准化考核汇总数据");
    }

    /**
     * 获取标准化考核汇总详细信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:query')")
    @GetMapping(value = "/{inspectionId}")
    public AjaxResult getInfo(@PathVariable("inspectionId") String inspectionId)
    {
        return success(inspectionResultService.selectInspectionResultByInspectionId(inspectionId));
    }

    /**
     * 新增标准化考核汇总
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:add')")
    @Log(title = "标准化考核汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionAddVo inspectionAddVo)
    {
        return toAjax(inspectionResultService.addInspectionResult(inspectionAddVo));
    }

    /**
     * 修改标准化考核汇总
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:edit')")
    @Log(title = "标准化考核汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionResult inspectionResult)
    {
        inspectionResult.setInspectionState(9L);
        return toAjax(inspectionResultService.updateInspectionResult(inspectionResult));
    }

    /**
     * 删除标准化考核汇总
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:inspectionResult:remove')")
    @Log(title = "标准化考核汇总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inspectionIds}")
    public AjaxResult remove(@PathVariable String[] inspectionIds)
    {
        return toAjax(inspectionResultService.deleteInspectionResultByInspectionIds(inspectionIds));
    }
}
